<template>
  <el-dialog v-model="visible" title="登录" width="20%" draggable>
    <div class="logincontainer">
      <h1>用户注册</h1>
      <div class="loginform">
        <div class="loginitem">
          <label for="phoneNumID">手机号</label><input class="input" v-model="phonenNum" autofocus size="mini" id="phoneNumID" style="width:'20%'" placeholder="请输入手机号">
                <!-- v-model把输入的值传输给name变量 -->
                <br/>
        </div>
        <div class="loginitem">
          <label for="loginPassID">密码</label><input class="input" v-model="loginPass" autofocus id="loginPassID" placeholder="请输入登录密码" :type="showPassword ? 'text' : 'password'">
                <!-- v-model把输入的值传输给name变量 -->
                <br/>
        </div>
    </div>
    </div>

    <template #footer>
      <span class="dialog-footer">
        <el-button class="loginbutton" @click="visible = false">取消</el-button>
        <el-button class="loginbutton" type="primary" @click="onConfirm">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>
    
<script  setup>
import CryptoJS from 'crypto-js';
import { ref } from 'vue'
import { ElMessage } from 'element-plus';
// import axios from 'axios';
import { useStore } from 'vuex';
const store = useStore();

const visible = ref(false)
const loginPass = ref("")
const phonenNum = ref("13631218234")
const showPassword = ref(false)
var loginDlgParent = null

const onConfirm = () => {
  console.log("login dlg show--------------:");

  const hashedPassword = CryptoJS.SHA256(loginPass.value).toString();
  let data = {
    password: hashedPassword,
    phoneNum: phonenNum.value
  };

  let uid = store.state.uid;
  // store.dispatch("login", data);
  uid = store.state.uid;
  
  post2Login(data);
  visible.value = false;
}


var post2Login = function (data) {
  console.log("store login******************");
  var url = "api/user/login";
  axios.post(url, data).then(res => {
    console.log(res.data);
    let rstObj = res.data;
    if (rstObj.ret == "success") {
      let user = {
        uid: rstObj.data.uid,
        name: rstObj.data.name,
        token: rstObj.data.token
      };
      store.commit("updateUser", user);
      if (loginDlgParent){
        loginDlgParent(true, rstObj.data.name);
      }
      ElMessage('登录成功');
    }
    else {
      if (loginDlgParent){
        loginDlgParent(false,"");
      }
      let errMsg = rstObj.errMsg;
      ElMessage('登录失败:'+errMsg);
    }
  }).catch(function (error) {
    if (loginDlgParent){
        loginDlgParent(false,"");
      }
    console.log(error);
    ElMessage('登录失败');
  })
}

var setObserver = function(observer){
  loginDlgParent = observer;
}

defineExpose({
  visible,
  setObserver
});

</script>
  
<style>
#background{
    width: 100%;
    height: 100%;
    background-color: #4e5ef3;
    background-size:100% 100%;
    position: fixed;
    top: 0;
    left: 0;
}

.transparent-bg .el-dialog__body {
  /* 设置背景透明 */
  background: transparent;
  /* 设置内容透明度 */
  color: rgba(0, 0, 0, 0.7);
}

.logincontainer{
  width: 420px;
  height: 320px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  background:#414883;
  text-align: center;
  border-radius: 20px;
  margin-top: 10px;
}
.logincontainer h1{
  color: aliceblue;
  margin-left: 20px;
}
.loginitem {
  color: white;
  margin-left: 15%;
  margin-top: 35px;
  font-size: 16px;
  text-align: left;
}
.loginitem label{
  float:left;
  width: 5em;
  margin-right: 1em;
  text-align: justify;
  text-align-last: justify;
}
.logininput{
  margin-left: -5px;
  padding: 4px;
  border: solid 1px #4e5ef3;
  outline: 0;
  font: normal 13px/100% Verdana,Tahoma,sans-serif;
  width: 200px;
  height: 23px;
  background:#f1f1f190;
  box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 8px;
}
.loginbutton{
  position: relative;
  height: 33px;
  width: 100px;
  background: rgba(35, 19, 252, 0.425);
  border-radius: 10px;
  margin-top: 48px;
  box-shadow: none;
  color: white;
  margin-left: 40px;
  margin-right: 10px;

}
</style>
    